package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.LateOrderAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.OrderBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.PositionInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.view.CommonHeader;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyOrderActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private LateOrderAdapter mAdapter;
    private int page=1;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.LatelyOrder,this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        OrderBean parentBean= DealResult.getInstance().dealDatas(MyOrderActivity.this,response
                                ,new TypeToken<OrderBean>() {}.getType());
                        if(parentBean==null){return;}
                        if(parentBean.getCode()== Constant.SUCESS){
                            setDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(MyOrderActivity.this,parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(MyOrderActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<OrderBean.BodyBean> body) {
        mAdapter.addAll(body);
        recyclerView.refreshComplete(mAdapter.getItemCount());
    }

    private void initView() {
        tvMiddleTitle.setText("我的订单");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyOrderActivity.this));
        mAdapter=new LateOrderAdapter(MyOrderActivity.this, new PositionInter() {
            @Override
            public void callBack(int position) {
                refresh();
            }
        });
        mAdapter.setDataList(new ArrayList<OrderBean.BodyBean>());
         lRecyclerViewAdapter=new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(MyOrderActivity.this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        addHeader(lRecyclerViewAdapter);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(MyOrderActivity.this, OrderDetailActivity.class));
            }
        });
    }

    private void addHeader(LRecyclerViewAdapter lRecyclerViewAdapter) {
        CommonHeader header=new CommonHeader(MyOrderActivity.this,R.layout.header_one_fragment);
        RelativeLayout tvMasterAll=header.findViewById(R.id.tv_master_all);
        TextView tv_refuse=header.findViewById(R.id.tv_refuse);
        TextView tv_accept=header.findViewById(R.id.tv_accept);
        TextView tv_evaluate=header.findViewById(R.id.tv_evaluate);
        final Intent i=new Intent(MyOrderActivity.this, OrderMasterActivity.class);
        tv_refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i.putExtra("flag",1);
                startActivity(i);
            }
        });
        tv_accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i.putExtra("flag",2);
                startActivity(i);
            }
        });
        tv_evaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i.putExtra("flag",3);
                startActivity(i);
            }
        });
//
        TextView tv_refuse1=header.findViewById(R.id.tv_refuse1);
        TextView tv_accept1=header.findViewById(R.id.tv_accept1);
        TextView tv_evaluate1=header.findViewById(R.id.tv_evaluate1);
        final Intent i1=new Intent(MyOrderActivity.this, OrderEmployeActivity.class);
        tv_refuse1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i1.putExtra("flag",1);
                startActivity(i1);
            }
        });
        tv_accept1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i1.putExtra("flag",2);
                startActivity(i1);
            }
        });
        tv_evaluate1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i1.putExtra("flag",3);
                startActivity(i1);
            }
        });
        tvMasterAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyOrderActivity.this, OrderMasterActivity.class));
            }
        });
        RelativeLayout tvEmployeAll=header.findViewById(R.id.tv_employe_all);
        tvEmployeAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyOrderActivity.this, OrderEmployeActivity.class));
            }
        });

        lRecyclerViewAdapter.addHeaderView(header);
    }


    @OnClick(R.id.title_left_one_btn)
    public void onViewClicked() {
        this.finish();
    }
    private void refresh(){
        page=1;
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        getDatas();
    }
}


